[id].vue 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <template>
  2. <div class="admin--event-form">
  3. <div v-if="isLoading" class="admin--loading">
  4. 데이터를 불러오는 중...
  5. </div>
  6. <form v-else @submit.prevent="handleSubmit" class="admin--form">
  7. <!-- 카테고리 -->
  8. <div class="admin--form-group">
  9. <label class="admin--form-label">카테고리 <span class="admin--required">*</span></label>
  10. <select v-model="formData.category" class="admin--form-select" required>
  11. <option value="">카테고리를 선택하세요</option>
  12. <option value="진행중">진행중</option>
  13. <option value="완료">완료</option>
  14. </select>
  15. </div>
  16. <!-- 댓글허용 -->
  17. <div class="admin--form-group">
  18. <label class="admin--form-label">댓글허용</label>
  19. <div class="admin--checkbox-group">
  20. <label class="admin--checkbox-label">
  21. <input v-model="formData.allow_comment" type="checkbox">
  22. <span>댓글 허용</span>
  23. </label>
  24. </div>
  25. </div>
  26. <!-- 공지 -->
  27. <div class="admin--form-group">
  28. <label class="admin--form-label">공지</label>
  29. <div class="admin--checkbox-group">
  30. <label class="admin--checkbox-label">
  31. <input v-model="formData.is_notice" type="checkbox">
  32. <span>공지글로 등록</span>
  33. </label>
  34. </div>
  35. </div>
  36. <!-- 이름 -->
  37. <div class="admin--form-group">
  38. <label class="admin--form-label">이름 <span class="admin--required">*</span></label>
  39. <input
  40. v-model="formData.name"
  41. type="text"
  42. class="admin--form-input"
  43. placeholder="이름을 입력하세요"
  44. required
  45. >
  46. </div>
  47. <!-- 이메일 -->
  48. <div class="admin--form-group">
  49. <label class="admin--form-label">이메일 <span class="admin--required">*</span></label>
  50. <input
  51. v-model="formData.email"
  52. type="email"
  53. class="admin--form-input"
  54. placeholder="이메일을 입력하세요"
  55. required
  56. >
  57. </div>
  58. <!-- 기간 -->
  59. <div class="admin--form-group">
  60. <label class="admin--form-label">기간 <span class="admin--required">*</span></label>
  61. <div class="admin--date-range">
  62. <input
  63. v-model="formData.start_date"
  64. type="date"
  65. class="admin--form-input"
  66. required
  67. >
  68. <span class="admin--date-separator">~</span>
  69. <input
  70. v-model="formData.end_date"
  71. type="date"
  72. class="admin--form-input"
  73. required
  74. >
  75. </div>
  76. </div>
  77. <!-- 제목 -->
  78. <div class="admin--form-group">
  79. <label class="admin--form-label">제목 <span class="admin--required">*</span></label>
  80. <input
  81. v-model="formData.title"
  82. type="text"
  83. class="admin--form-input"
  84. placeholder="제목을 입력하세요"
  85. required
  86. >
  87. </div>
  88. <!-- 내용 -->
  89. <div class="admin--form-group">
  90. <label class="admin--form-label">내용 <span class="admin--required">*</span></label>
  91. <SunEditor v-model="formData.content" />
  92. </div>
  93. <!-- 기존 첨부파일 -->
  94. <div v-if="existingFiles.length > 0" class="admin--form-group">
  95. <label class="admin--form-label">기존 첨부파일</label>
  96. <div class="admin--file-list">
  97. <div v-for="(file, index) in existingFiles" :key="'existing-' + index" class="admin--file-item">
  98. <a :href="file.url" target="_blank" class="admin--file-name">{{ file.name }}</a>
  99. <span class="admin--file-size">({{ formatFileSize(file.size) }})</span>
  100. <button type="button" class="admin--btn-remove-file" @click="removeExistingFile(index)">
  101. 삭제
  102. </button>
  103. </div>
  104. </div>
  105. </div>
  106. <!-- 새 파일첨부 -->
  107. <div class="admin--form-group">
  108. <label class="admin--form-label">파일첨부</label>
  109. <div v-if="attachedFiles.length > 0" class="admin--file-list">
  110. <div v-for="(file, index) in attachedFiles" :key="'new-' + index" class="admin--file-item">
  111. <span class="admin--file-name">{{ file.name }}</span>
  112. <span class="admin--file-size">({{ formatFileSize(file.size) }})</span>
  113. <button type="button" class="admin--btn-remove-file" @click="removeFile(index)">
  114. 삭제
  115. </button>
  116. </div>
  117. </div>
  118. <input
  119. ref="fileInput"
  120. type="file"
  121. multiple
  122. class="admin--form-file-hidden"
  123. @change="handleFileAdd"
  124. >
  125. <button type="button" class="admin--btn admin--btn-secondary" @click="triggerFileInput">
  126. 파일 추가
  127. </button>
  128. </div>
  129. <!-- 버튼 영역 -->
  130. <div class="admin--form-actions">
  131. <button
  132. type="submit"
  133. class="admin--btn admin--btn-primary"
  134. :disabled="isSaving"
  135. >
  136. {{ isSaving ? '저장 중...' : '확인' }}
  137. </button>
  138. <button
  139. type="button"
  140. class="admin--btn admin--btn-secondary"
  141. @click="goToList"
  142. >
  143. 목록
  144. </button>
  145. </div>
  146. <!-- 성공/에러 메시지 -->
  147. <div v-if="successMessage" class="admin--alert admin--alert-success">
  148. {{ successMessage }}
  149. </div>
  150. <div v-if="errorMessage" class="admin--alert admin--alert-error">
  151. {{ errorMessage }}
  152. </div>
  153. </form>
  154. </div>
  155. </template>
  156. <script setup>
  157. import { ref, onMounted } from 'vue'
  158. import { useRoute, useRouter } from 'vue-router'
  159. import SunEditor from '~/components/admin/SunEditor.vue'
  160. definePageMeta({
  161. layout: 'admin',
  162. middleware: ['auth']
  163. })
  164. const route = useRoute()
  165. const router = useRouter()
  166. const { get, put, upload } = useApi()
  167. const isLoading = ref(true)
  168. const isSaving = ref(false)
  169. const successMessage = ref('')
  170. const errorMessage = ref('')
  171. const attachedFiles = ref([])
  172. const existingFiles = ref([])
  173. const fileInput = ref(null)
  174. const formData = ref({
  175. category: '',
  176. allow_comment: false,
  177. is_notice: false,
  178. name: '',
  179. email: '',
  180. start_date: '',
  181. end_date: '',
  182. title: '',
  183. content: '',
  184. file_urls: []
  185. })
  186. const loadEvent = async () => {
  187. isLoading.value = true
  188. const id = route.params.id
  189. const { data, error } = await get(`/board/event/${id}`)
  190. if (data) {
  191. formData.value = {
  192. category: data.category || '',
  193. allow_comment: data.allow_comment || false,
  194. is_notice: data.is_notice || false,
  195. name: data.name || '',
  196. email: data.email || '',
  197. start_date: data.start_date || '',
  198. end_date: data.end_date || '',
  199. title: data.title || '',
  200. content: data.content || '',
  201. file_urls: data.file_urls || []
  202. }
  203. existingFiles.value = data.file_urls || []
  204. }
  205. isLoading.value = false
  206. }
  207. const triggerFileInput = () => {
  208. fileInput.value?.click()
  209. }
  210. const handleFileAdd = (event) => {
  211. const files = Array.from(event.target.files)
  212. attachedFiles.value.push(...files)
  213. event.target.value = ''
  214. }
  215. const removeFile = (index) => {
  216. attachedFiles.value.splice(index, 1)
  217. }
  218. const removeExistingFile = (index) => {
  219. existingFiles.value.splice(index, 1)
  220. }
  221. const formatFileSize = (bytes) => {
  222. if (bytes === 0) return '0 Bytes'
  223. const k = 1024
  224. const sizes = ['Bytes', 'KB', 'MB', 'GB']
  225. const i = Math.floor(Math.log(bytes) / Math.log(k))
  226. return Math.round(bytes / Math.pow(k, i) * 100) / 100 + ' ' + sizes[i]
  227. }
  228. const handleSubmit = async () => {
  229. successMessage.value = ''
  230. errorMessage.value = ''
  231. if (!formData.value.category) {
  232. errorMessage.value = '카테고리를 선택하세요.'
  233. return
  234. }
  235. if (!formData.value.title) {
  236. errorMessage.value = '제목을 입력하세요.'
  237. return
  238. }
  239. if (!formData.value.content) {
  240. errorMessage.value = '내용을 입력하세요.'
  241. return
  242. }
  243. if (!formData.value.start_date || !formData.value.end_date) {
  244. errorMessage.value = '기간을 입력하세요.'
  245. return
  246. }
  247. isSaving.value = true
  248. try {
  249. let fileUrls = [...existingFiles.value]
  250. // 새 파일 업로드
  251. if (attachedFiles.value.length > 0) {
  252. for (const file of attachedFiles.value) {
  253. const formDataFile = new FormData()
  254. formDataFile.append('file', file)
  255. const { data: uploadData, error: uploadError } = await upload('/upload/file', formDataFile)
  256. if (uploadError) {
  257. errorMessage.value = `파일 업로드에 실패했습니다: ${file.name}`
  258. isSaving.value = false
  259. return
  260. }
  261. fileUrls.push({
  262. name: file.name,
  263. url: uploadData.url,
  264. size: file.size
  265. })
  266. }
  267. }
  268. const submitData = {
  269. ...formData.value,
  270. file_urls: fileUrls
  271. }
  272. const id = route.params.id
  273. const { data, error } = await put(`/board/event/${id}`, submitData)
  274. if (error) {
  275. errorMessage.value = error.message || '수정에 실패했습니다.'
  276. } else {
  277. successMessage.value = '이벤트가 수정되었습니다.'
  278. setTimeout(() => {
  279. router.push('/admin/board/event')
  280. }, 1000)
  281. }
  282. } catch (error) {
  283. errorMessage.value = '서버 오류가 발생했습니다.'
  284. console.error('Save error:', error)
  285. } finally {
  286. isSaving.value = false
  287. }
  288. }
  289. const goToList = () => {
  290. router.push('/admin/board/event')
  291. }
  292. onMounted(() => {
  293. loadEvent()
  294. })
  295. </script>